<script>

import { GET_LOGIN_ID, PAGE_INFO } from '@/utils/MyUtils'
import BlogDetail from '@/views/admin/blog/BlogDetail.vue'
import BlogTag from '@/views/admin/blog/BlogTag.vue'
import BlogComment from '@/views/admin/blog/BlogComment.vue'

export default {
  name: 'BlogList',
  components: { BlogComment, BlogTag, BlogDetail },
  data () {
    return {
      type: '',
      searchInfo: {
        createUserId: '',
        applyTimeRange: '',
        blogStatus: '',
        categoryId: '',
        hasComment: '',
        status: '',
        title: ''
      },
      pageInfo: {
        ...PAGE_INFO
      },
      applyTime: [],
      tableList: [],
      categoryList: [],
      delIds: [],
      userinfoList: [],
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick (picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick (picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick (picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      }
    }
  },
  methods: {
    init (createUserId) {
      this.tableList = []
      this.pageInfo = { ...this.$options.data().pageInfo }
      this.searchInfo = { ...this.$options.data().searchInfo }
      this.applyTime = []
      this.type = this.$route.meta.type
      console.log(createUserId)
      if (createUserId) {
        this.searchInfo.createUserId = createUserId
      } else {
        if (this.type === 'vip') {
          this.searchInfo.createUserId = GET_LOGIN_ID()
        } else {
          this.searchInfo.createUserId = ''
        }
      }
      this.getCategoryList()
      this.getTableList()
    },
    getTableList () {
      if (this.applyTime?.length > 0) {
        this.searchInfo.applyTimeRange = this.applyTime.toString()
      }
      this.$axios.get('blog/page', { params: { ...this.pageInfo, ...this.searchInfo } }).then(data => {
        this.tableList = [...data.records]
        this.pageInfo.count = data.total
      })
    },
    currentChange (value) {
      this.pageInfo.page = value
      this.getTableList()
    },
    sizeChange (value) {
      this.pageInfo.page = 1
      this.pageInfo.limit = value
      this.getTableList()
    },
    selectionChange (selection) {
      this.delIds = [...selection.map(item => item.blogId)]
    },
    search () {
      this.pageInfo.page = 1
      this.getTableList()
    },
    clear () {
      this.pageInfo = { ...this.$options.data().pageInfo }
      this.searchInfo = { ...this.$options.data().searchInfo }
      this.init()
    },
    deleteData (id) {
      if (!id && this.delIds.length === 0) {
        this.$alert('请至少选中1条进行删除！', { type: 'warning' })
        return false
      }
      const deleteIds = id ? [id] : [...this.delIds]
      this.$confirm(`确定删除[${deleteIds}]的数据吗?`, { type: 'info', title: '询问' }).then(() => {
        this.$axios.delete('blog/delete', { data: deleteIds }).then(() => {
          this.$message.success('操作成功！')
          this.getTableList()
        }).catch(e => e)
      })
    },
    statusChange (id) {
      this.$confirm(`确定更改编号【${id}】的状态吗?`, { type: 'info', title: '询问' }).then(() => {
        this.$axios.post('blog/status/' + id).then(() => {
          this.$message.success('操作成功！')
          this.getTableList()
        }).catch(e => e)
      }).catch(e => e)
    },
    getCategoryList () {
      this.$axios.get('category/select', { params: { type: this.type === 'vip' ? 'vip' : '' } }).then(data => {
        this.categoryList = [...data]
      }).catch(e => e)
    },
    getUserinfoList () {
      this.$axios.get('userinfo/select').then(data => {
        this.userinfoList = [...data]
      }).catch(e => e)
    },
    editData (blogId) {
      this.$router.push('/home/vip/apply?blogId=' + blogId)
    },
    preview (blogId) {
      this.$refs.blogViewRef.init(blogId)
    },
    comment (blogId) {
      this.$refs.blogCommentRef.init(blogId)
    },
    tag (blogId) {
      this.$refs.blogTagRef.init(blogId)
    }
  },
  watch: {
    '$route.path': function () {
      this.init()
    }
  },
  created () {
    this.$nextTick(() => {
      this.init()
      this.getUserinfoList()
    })
  }
}
</script>

<template>
  <div>
    <el-card>
      <el-form :model="searchInfo" inline label-width="80px">
        <el-form-item label="标题">
          <el-input v-model="searchInfo.title" placeholder="标题" clearable/>
        </el-form-item>
        <el-form-item label="发布时间">
          <el-date-picker
            v-model="applyTime"
            type="datetimerange"
            value-format="yyyy-MM-dd HH:mm:ss"
            :picker-options="pickerOptions"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            align="right">
          </el-date-picker>
        </el-form-item>
        <el-form-item v-if="type==='all'" label="创建人">
          <el-select v-model="searchInfo.createUserId" placeholder="选择创建人" clearable filterable>
            <el-option v-for="userinfo in userinfoList" :key="userinfo.userinfoId" :value="userinfo.userinfoId" :label="userinfo.username"/>
          </el-select>
        </el-form-item>
        <el-form-item label="分类">
          <el-select v-model="searchInfo.categoryId" placeholder="选择分类" clearable filterable>
            <el-option v-for="category in categoryList" :key="category.categoryId" :value="category.categoryId" :label="category.name"/>
          </el-select>
        </el-form-item>
        <el-form-item label="是否评论">
          <el-select v-model="searchInfo.hasComment" placeholder="选择评论" clearable>
            <el-option :value="1" label="允许" />
            <el-option :value="2" label="禁止" />
          </el-select>
        </el-form-item>
        <el-form-item label="博客状态">
          <el-select v-model="searchInfo.blogStatus" placeholder="选择博客状态" clearable>
            <el-option :value="1" label="发布" />
            <el-option :value="2" label="草稿" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="searchInfo.status" placeholder="选择状态" clearable>
            <el-option :value="1" label="可用" />
            <el-option :value="2" label="禁用" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="search">查询</el-button>
          <el-button @click="clear">清空</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
      <el-button @click="deleteData()">删除</el-button>
      <el-table @selection-change="selectionChange" :data="tableList" border>
        <el-table-column type="selection" fixed="left"/>
        <el-table-column prop="blogId" label="编号"/>
        <el-table-column prop="title" label="标题" show-overflow-tooltip/>
        <el-table-column prop="category.name" label="分类名"/>
        <el-table-column v-if="type==='all'" prop="createUserinfo.username" label="创建人"/>
        <el-table-column prop="applyTime" label="发布时间"/>
        <el-table-column label="博客状态">
          <template slot-scope="{row}">
            <el-tag type="success" v-if="row.blogStatus === 1">发布</el-tag>
            <el-tag type="warning" v-else>草稿</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="状态">
          <template slot-scope="{row}">
            <el-button @click="statusChange(row.blogId)" title="点击切换状态" type="success" v-if="row.status === 1">可用</el-button>
            <el-button @click="statusChange(row.blogId)" title="点击切换状态" type="danger" v-else>禁用</el-button>
          </template>
        </el-table-column>
        <el-table-column width="180px" label="数据">
          <template slot-scope="{row}">
            <el-button @click="tag(row.blogId)">标签</el-button>
            <el-button @click="comment(row.blogId)">评论</el-button>
          </template>
        </el-table-column>
        <el-table-column fixed="right" width="270px" label="操作">
          <template slot-scope="{row}">
            <el-button v-if="type === 'vip'" @click="editData(row.blogId)">修改</el-button>
            <el-button @click="deleteData(row.blogId)">删除</el-button>
            <el-button @click="preview(row.blogId)">预览</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination :current-page="pageInfo.page" :page-size="pageInfo.limit" :page-sizes="[5,10,20,50,100]" :total="pageInfo.count" layout="total,prev,pager,next,jumper,sizes" @current-change="currentChange" @size-change="sizeChange"/>
    </el-card>
    <blog-detail ref="blogViewRef"/>
    <blog-tag ref="blogTagRef"/>
    <blog-comment ref="blogCommentRef"/>
  </div>
</template>

<style scoped lang="scss">

</style>
